package com.moehu.back.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.moehu.back.dao.CommentDao;
import com.moehu.back.dao.OpusDao;
import com.moehu.back.model.CommentModel;
import com.moehu.back.model.ResultModel;
import com.moehu.back.service.CommentService;
import com.moehu.back.service.TokenService;
import com.moehu.back.utils.ResultUtils;
import com.moehu.back.utils.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CommentServiceImpl extends BaseServiceImpl implements CommentService {

    @Autowired
    CommentDao commentDao;

    @Autowired
    OpusDao opusDao;

    @Autowired
    TokenService tokenService;

    @Override
    public ResultModel pushComment(CommentModel comment) {
        comment.setUser_id(tokenService.uid());
        Integer inster = commentDao.add(comment);
        if (inster != 0) {
            return ResultUtils.SuccessStatus("发布成功");
        } else {
            return ResultUtils.Error("发布失败");
        }
    }

    @Override
    public ResultModel all(Map<String, Integer> opus) {
        Page page = PageHelper.startPage(opus.get("page"), opus.get("count"));
        List<CommentModel> commentList = opusDao.getUserComment(
                opus.get("opus_id"),
                opus.get("dynamic_id"),
                opus.get("comment_type")
        );
        commentList = getComment(commentList);

        if (commentList.isEmpty()) {
            return ResultUtils.Error("没有评论");
        } else {
            return ResultUtils.Success(Tool.CountPage(page, commentList));
        }
    }


    @Override
    public ResultModel delete(Map<String, Integer> comment) {
        Integer deleteId = commentDao.delete(comment.get("level"), comment.get("opus_id"),comment.get("comment_id"));
        if (deleteId != 0) {
            return ResultUtils.SuccessStatus("删除成功");
        } else {
            return ResultUtils.Error("删除失败");
        }
    }

    @Override
    public ResultModel report(Map<String, Integer> comment) {
        Integer reportId = commentDao.report(comment.get("comment_id"));
        log.info("reportId ======== {} ", reportId);
        return null;
    }
}
